﻿using System.Reflection;
using Autofac;
using Autofac.Extras.DynamicProxy;
using Scheduler.Common.AOP;

namespace Scheduler.Common.Extensions;

public class AutofacModuleRegister : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var basePath = AppContext.BaseDirectory;
        var servicesDllFile = Path.Combine(basePath, "Scheduler.Services.dll");
        if (!(File.Exists(servicesDllFile)))
        {
            var msg = "service.dll 丢失，因为项目解耦了，所以需要先F6编译，再F5运行，请检查 bin 文件夹，并拷贝。";
            //Log.Error(msg);
            throw new Exception(msg);
        }
        // 注册拦截器
        builder.RegisterType<LogAOP>();
       builder.RegisterType<CacheAOP>();
        // 获取 Service.dll 程序集服务，并注册
        var assemblysServices = Assembly.LoadFrom(servicesDllFile);
        builder.RegisterAssemblyTypes(assemblysServices)
            .AsImplementedInterfaces()
            .InstancePerDependency()
            .EnableInterfaceInterceptors();
       // InterceptedBy(typeof(CacheAOP));
    }
}
